package com.ruoyi.electricity.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.electricity.domain.DTO.DataStatusCount;
import com.ruoyi.electricity.domain.DTO.StatusCount;
import com.ruoyi.electricity.domain.TDataGatewaySystem;
import com.ruoyi.electricity.mapper.TDataGatewaySystemMapper;
import com.ruoyi.electricity.service.ITDataGatewaySystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 网关数据系统Service业务层处理
 *
 * @author wu
 * @date 2023-10-28
 */
@Service
public class TDataGatewaySystemServiceImpl implements ITDataGatewaySystemService {
    @Autowired
    private TDataGatewaySystemMapper tDataGatewaySystemMapper;

    /**
     * 查询网关数据系统
     *
     * @param id 网关数据系统ID
     * @return 网关数据系统
     */
    @Override
    public TDataGatewaySystem selectTDataGatewaySystemById(Long id) {
        return tDataGatewaySystemMapper.selectTDataGatewaySystemById(id);
    }

    /**
     * 查询网关数据系统列表
     *
     * @param tDataGatewaySystem 网关数据系统
     * @return 网关数据系统
     */
    @Override
    public List<TDataGatewaySystem> selectTDataGatewaySystemList(TDataGatewaySystem tDataGatewaySystem) {
        return tDataGatewaySystemMapper.selectTDataGatewaySystemList(tDataGatewaySystem);
    }

    /**
     * 新增网关数据系统
     *
     * @param tDataGatewaySystem 网关数据系统
     * @return 结果
     */
    @Override
    public int insertTDataGatewaySystem(TDataGatewaySystem tDataGatewaySystem) {
        return tDataGatewaySystemMapper.insertTDataGatewaySystem(tDataGatewaySystem);
    }

    /**
     * 修改网关数据系统
     *
     * @param tDataGatewaySystem 网关数据系统
     * @return 结果
     */
    @Override
    public int updateTDataGatewaySystem(TDataGatewaySystem tDataGatewaySystem) {
        return tDataGatewaySystemMapper.updateTDataGatewaySystem(tDataGatewaySystem);
    }

    /**
     * 批量删除网关数据系统
     *
     * @param ids 需要删除的网关数据系统ID
     * @return 结果
     */
    @Override
    public int deleteTDataGatewaySystemByIds(Long[] ids) {
        return tDataGatewaySystemMapper.deleteTDataGatewaySystemByIds(ids);
    }

    /**
     * 删除网关数据系统信息
     *
     * @param id 网关数据系统ID
     * @return 结果
     */
    @Override
    public int deleteTDataGatewaySystemById(Long id) {
        return tDataGatewaySystemMapper.deleteTDataGatewaySystemById(id);
    }

    @Override
    public List<StatusCount> getStatusCount(Date startTime, Date endTime) {
        List<String> statusList = tDataGatewaySystemMapper.selectAllStatus();
        if (!statusList.contains("安全")){
            statusList.add("安全");
        }
        if (!statusList.contains("存在风险")){
            statusList.add("存在风险");
        }
        if (!statusList.contains("不安全")){
            statusList.add("不安全");
        }
        List<StatusCount> statusCountList = tDataGatewaySystemMapper.getStatusCount(startTime, endTime);
        ArrayList<String> list = new ArrayList<>();
        for (StatusCount statusCount : statusCountList) {
            list.add(statusCount.getHiddenSate());
        }
        for (String s : statusList) {
            if (!list.contains(s)) {
                StatusCount statusCount = new StatusCount();
                statusCount.setHiddenSate(s);
                statusCount.setCount("0");
                statusCountList.add(statusCount);
            }
        }

        return statusCountList;
    }

    @Override
    public List<DataStatusCount> getDataStatusCount(Date startTime, Date endTime) {
        List<String> statusList = tDataGatewaySystemMapper.selectAllDataStatus();
        List<DataStatusCount> dataStatusCountList = tDataGatewaySystemMapper.getDataStatusCount(startTime, endTime);
        if (!statusList.contains("开启")){
            statusList.add("开启");
        }
        if (!statusList.contains("离线")){
            statusList.add("离线");
        }
        if (!statusList.contains("停止")){
            statusList.add("停止");
        }
        ArrayList<String> list = new ArrayList<>();
        for (DataStatusCount count : dataStatusCountList) {
            list.add(count.getDataStatus());
        }
        for (String s : statusList) {
            if (!list.contains(s)) {
                DataStatusCount count = new DataStatusCount();
                count.setDataStatus(s);
                count.setCount("0");
                dataStatusCountList.add(count);
            }
        }

        return dataStatusCountList;

    }
}
